package ui.map 
{
	import com.gaiaframework.api.Gaia;
	import com.gaiaframework.api.IBitmapSprite;
	import com.gaiaframework.assets.SpriteAsset;
	import com.greensock.TimelineLite;
	import com.greensock.TweenAlign;
	import com.greensock.TweenMax;
	import com.greensock.easing.*;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import model.Model;
	import pages.Pages;
	import ui.map.InertiaBehindMask;
	import utils.ShapeUtil;
	import utils.TextFieldUtil;
	
	/**
	 * ...
	 * @author usual
	 */
	public class Marker extends MovieClip 
	{
		private var _markerClip:MarkerClip;
		private var _timelineApear:TimelineLite;
		private var _timeLine:TimelineLite;
		private var _title:TextField;
		private var _inertiaBehindMask:InertiaBehindMask;
		private var _spriteAsset:SpriteAsset;
		public function Marker() 
		{
			addEventListener(Event.ADDED_TO_STAGE, build);
		}
		
		public function resize():void 
		{
			trace("[Marker.resize] ", this.parent.scaleX);
			
			//this.scaleX = this.scaleY = 1;
			if (this.parent.scaleX == 1) this.scaleX = this.scaleY = .8;
			if (this.parent.scaleX == 1.5) this.scaleX = this.scaleY = .6;
			if (this.parent.scaleX == 2) this.scaleX = this.scaleY = .4;
		}
		
		private function build(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, build);
			
			setMarker();
			reparentAsset();
			addTitle();
			
			this.scaleX = this.scaleY = .8;
		}
		
		private function reparentAsset():void 
		{
			
			_spriteAsset = SpriteAsset(Gaia.api.getPage(Pages.HOME).assets[this.name]);
			_spriteAsset.mouseEnabled = false;
			IBitmapSprite(_spriteAsset).smoothing = true;
			_spriteAsset.visible = true;
			_spriteAsset.alpha = 0;
			_spriteAsset.scaleX = _spriteAsset.scaleY = 1.4;
			_spriteAsset.x = -_spriteAsset.width / 2;
			_spriteAsset.y = -_spriteAsset.height / 2;
			
			addChild(_spriteAsset.container);
			var c:Shape = ShapeUtil.getCircle(235);
			c.x= 0
			c.y= 0
			addChild(c);
			_spriteAsset.mask = c;
		}
		
		private function addInertia():void 
		{
			//_inertiaBehindMask = new InertiaBehindMask(this.name);
			//addChild(_inertiaBehindMask);
		}
		
		private function addTitle():void 
		{
			_title = new TextField();
			_title.selectable = false;
			_title.wordWrap = false;
			_title.multiline = true;
			TextFieldUtil.setText(_title, "<marker>" + Model.instance.getUiLabel(this.name + '-title') + "</marker>");
			_title.mouseEnabled = false;
			addChild(_title);
			_title.x = -_title.width / 2;
			_title.y = -_title.height / 2;
			_title.alpha = 0;
		}
		
		private function setMarker():void 
		{
			_markerClip = new MarkerClip();
			_markerClip.buttonMode = true;
			_markerClip.pastille_fond_noir.alpha = 0;
			_markerClip.pastille_fond_noir.scaleX = _markerClip.pastille_fond_noir.scaleY = .99;
			_markerClip.pastille_fond_blanc.scaleX = _markerClip.pastille_fond_blanc.scaleY = 0;
			_markerClip.mask_img.scaleX = _markerClip.mask_img.scaleY = 0;
			_markerClip.rondBlanc.scaleX = _markerClip.rondBlanc.scaleY = _markerClip.rondNoir.scaleX = _markerClip.rondNoir.scaleY = 0;
			_markerClip.croixHaut_mc.scaleX = _markerClip.croixDroite_mc.scaleX = 0;
			_markerClip.croixBas_mc.scaleX = _markerClip.croixGauche_mc.scaleX = -1;
			_markerClip.croixHaut_mc.scaleY = _markerClip.croixDroite_mc.scaleY = -1;
			_markerClip.croixBas_mc.scaleY = _markerClip.croixGauche_mc.scaleX = 0;
			_markerClip.zone_sensible.scaleY = _markerClip.zone_sensible.scaleX = 1;
			addChild(_markerClip);
			apear();
		}
		
		private function apear():void 
		{
			_timelineApear = new TimelineLite(
			{
				tweens:[
					TweenMax.to(_markerClip.rondBlanc, 0.4, {scaleX:1.8, scaleY:1.8, ease:Circ.easeOut}),
					TweenMax.to(_markerClip.rondNoir,0.4, {scaleX:1.8, scaleY:1.8, delay:0.3, ease:Circ.easeOut}),
					TweenMax.to(_markerClip.croixHaut_mc, 0.5, {x:-0.35, y:-2,scaleX:1, scaleY:1,delay:0.8,  ease:Circ.easeOut}),
					TweenMax.to(_markerClip.croixDroite_mc, 0.5, {x:1, y:-0.40,scaleX:1, scaleY:1, delay:1, ease:Circ.easeOut}),
					TweenMax.to(_markerClip.croixBas_mc, 0.5, {x:-0.15, y:0.35,scaleX:1, scaleY:1,delay:1.2,  ease:Circ.easeOut}),
					TweenMax.to(_markerClip.croixGauche_mc, 0.5, {x:-1.20, y:-0.40,scaleX:1, scaleY:1,delay:1.4,  ease:Circ.easeOut}),
					TweenMax.to(_markerClip.croixHaut_mc, 0.5, {delay:0.7,  ease:Circ.easeOut}),
					TweenMax.to(_markerClip.croixDroite_mc, 0.5, {delay:0.9,  ease:Circ.easeOut}),
					TweenMax.to(_markerClip.croixBas_mc, 0.5, { delay:1,  ease:Circ.easeOut}),
					TweenMax.to(_markerClip.croixGauche_mc, 0.5,{delay:1.2,  ease:Circ.easeOut}),
					TweenMax.to(_markerClip.rondNoir,0.5, {alpha:0, scaleX:1.3, scaleY:1.3, delay:1.4, ease:Circ.easeOut}),
					TweenMax.to(_markerClip.rondBlanc, 0.5, {alpha:0, scaleX:1.1, scaleY:1.1, delay:1.2, ease:Circ.easeOut}),
					TweenMax.to(_markerClip.pastille_fond_noir, 0.5, {alpha:1,delay:1.2, ease:Circ.easeOut})
				],
				align:TweenAlign.NORMAL,
				onReverseComplete:destroy,
				onComplete:apearComplete
			});
			
		}
		
		private function destroy():void 
		{
			Gaia.api.goto(Pages.TIMELINE + '/' + this.name);
		}
		
		private function disApear():void
		{
			_timelineApear.reverse();
		}
		
		private function apearComplete():void 
		{
			_timeLine = new TimelineLite(
			{
				tweens:[
					TweenMax.to(_markerClip.croixBas_mc, 1, {rotation:-360,y:200,delay:0.2, ease:Quint.easeInOut}),
					TweenMax.to(_markerClip.croixHaut_mc, 1, {rotation:-360, y:-200,delay:0.2, ease:Quint.easeInOut}),
					TweenMax.to(_markerClip.croixDroite_mc, 1, {rotation:360,x:200,delay:0.2, ease:Quint.easeInOut}),
					TweenMax.to(_markerClip.croixGauche_mc, 1, {rotation:360,x:-200, delay:0.2,ease:Quint.easeInOut}),
					TweenMax.to(_markerClip.zone_sensible, 0, {scaleX:9.5, scaleY:9.5}),
					TweenMax.to(_markerClip.pastille_fond_noir, 0.5, {scaleX:11.5, scaleY:11.5,  ease:Quint.easeOut}),
					TweenMax.to(_markerClip.pastille_fond_blanc, 0.5, {scaleX:11.5, scaleY:11.5,delay:0.2, ease:Quint.easeOut}),
					TweenMax.to(_title, 0.5, { alpha:1, delay:.2 } ),
					TweenMax.to(_spriteAsset, 0.5, { alpha:1, delay:.1 } )
				],
				align:TweenAlign.NORMAL,
				onStart:overComplete,
				onReverseComplete:reverseComplete
			});
			_timeLine.stop();
			_markerClip.zone_sensible.scaleY = _markerClip.zone_sensible.scaleX = 1;
			_markerClip.addEventListener(MouseEvent.ROLL_OVER, _over);
			_markerClip.addEventListener(MouseEvent.ROLL_OUT, _out);
			
		}
		
		private function overComplete():void 
		{
			this.buttonMode = true;
		}
		
		private function _up(e:MouseEvent):void 
		{
			_out();
			_markerClip.removeEventListener(MouseEvent.MOUSE_OVER, _over);
			_markerClip.removeEventListener(MouseEvent.MOUSE_OUT, _out);
			_timelineApear.timeScale = 2.4;
			TweenMax.delayedCall(.4, disApear);
		}
		
		private function reverseComplete():void 
		{
			this.buttonMode = false;	
			_markerClip.zone_sensible.scaleY = _markerClip.zone_sensible.scaleX = 1;
		}
		
		private function _out(e:MouseEvent=null):void 
		{
			_markerClip.removeEventListener(MouseEvent.MOUSE_UP, _up);
			_timeLine.timeScale = 2.4;
			_timeLine.reverse();
		}
		
		private function _over(e:MouseEvent):void 
		{
			_timeLine.timeScale = 1.5;
			_markerClip.buttonMode = false;
			_markerClip.addEventListener(MouseEvent.MOUSE_UP, _up);
			_markerClip.parent.parent.setChildIndex(_markerClip.parent, _markerClip.parent.parent.numChildren-1);
			_timeLine.play();
		}
		
	}

}